// pages/shop/detail.js
const htmlSnip =
`<div class="div_class">
  <h3>郑重承诺</h3>
  <p>
    购买越多，优惠越多，快点击下方购买按钮吧
  </p>
</div>
`

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    swiperData: {
      indicatorDots: true,
      vertical: false,
      autoplay: false,
      interval: 2000,
      duration: 500
    },
    product:{},
    showStatus: false,
    buyNum: 1,
    options: [],
    productRichText: htmlSnip,
    selectAttr: "",
    selectAttrIndex: 0,
    selectNum: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, '接收路由参数')
    this.queryProduct(options.id);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  close: function () {
      this.setData({
        showStatus: false
      })
  },
  selectModal: function() {
    this.setData({
      showStatus: true
    })
  },
  btnClick(e) {
      console.log(e)
      this.close()
  },
  numChange(e) {
    console.log("子组件传值过来了", e.detail.count)
    this.setData({
      selectNum: e.detail.count
    });
  },
  changeRedio(e) {
    console.log("子组件传值过来了2", e.detail)
    this.setData({
      selectAttr: e.detail.value
    });
    this.setData({
      selectAttrIndex: e.detail.index
    });
  },
  queryProduct(id) {
    const that = this;
    const param = {_id: id}
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'productapi',
      // 传给云函数的参数
      data: {
        method: "queryProductById",
        args: param
      },
      // 成功回调
      complete: (res)=>{
        console.log(res, "产品详细")
        let temp = res.result.rstData.attr.map(item => {
          return {
            label: item.attrName,
            value: item.attrName
          }
        });
        that.setData({
          product: res.result.rstData,
          options: temp,
          selectAttr: temp[0].label,
          selectAttrIndex: 0,
          selectNum: 1
        });
      }
    })
  },
  addBuyCar() {
    console.log(this.data.product, "添加购物车");
    let product = this.data.product;
    product.num = this.data.selectNum;
    product.attrName = this.data.selectAttr;
    app.addBuyCar(product);
  },
  buyBtn() {
    // 立即购买
    console.log(this.data,"立即购买")
    let product = this.data.product;
    product.num = this.data.selectNum;
    product.buyAttrName = this.data.selectAttr;

    let order = [product];
    let amount = product.num * Number(product.sellPrice);

    let orderStr = JSON.stringify(order);
    let url = "../order/confirm/confirmOrder?orderInfo="+ orderStr+"&totalAmount="+amount
    wx.navigateTo({
      url: url,
    })
  }
})